package web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.dao.DBHelper;


@WebServlet("/textServlet")
public class TextServlet extends HttpServlet{
    DBHelper db=new DBHelper();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("收到了get请求");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		String op=req.getParameter("op");
		PrintWriter out=resp.getWriter();
		
		if("findbrand".equals(op)){
			findbrand(req,resp,out);
		}else if("findGoodsByPage".equals(op)){
			findGoodsByPage(req,resp,out);
		}else if("findGoodsdetail".equals(op)){
			findGoodsdetail(req,resp,out);
		}
	}
	

	//商品详情
		private void findGoodsdetail(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
			
			int gno=Integer.parseInt(req.getParameter("gno"));

			
			String sql="select * from goodsinfo where gno="+gno;

			List<Map<String,String>> list=db.findAll(sql, null);

				
	           Gson gson=new Gson();
	           out.print(gson.toJson(list));
		}
	private void findbrand(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		String sql="select * from brand";
		List<Map<String, String>> list=db.findAll(sql, null);
		//System.out.println(list);
		Gson gson=new Gson();
		out.print(gson.toJson(list));
	}

	//分页查内容
		private void findGoodsByPage(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
			//首先，先获取到参数
			int tno=Integer.parseInt(req.getParameter("tno"));
			int page=Integer.parseInt(req.getParameter("page"));
			//假设一页5条数据
			int count=5;
			//得到最大页
			int size=findGoodsByTno(tno);
			int pages=size%count==0?size/count:size/count+1;
			
			String sql="select * from goodsinfo where tno="+tno+" limit "+(page-1)*count+","+count;
			//String sql="select * from goodsinfo where tno="+tno+"limit"+(page-1)*count+","+count;
			List<Map<String,String>> list=db.findAll(sql, null);
			//将我们的当前页，和最大页，也一起存放到list里面去
			Map<String,String> maps=new HashMap<String,String>();
			maps.put("page", page+"");
			maps.put("pages", pages+"");
			//把map添加到list里面去
			list.add(maps);
			//最后，转化为json输出
			Gson gson=new Gson();
			out.print( gson.toJson(list));
			
			
		}
		//根据tno来查找总共有多少数据
		private int findGoodsByTno(int tno) {
			String sql="select * from goodsinfo where tno="+tno;
			List<Map<String,String>> list=db.findAll(sql, null);
	        return list.size();     
		}
	
		
	}
